package scope;

import java.util.Hashtable;
import java.util.Vector;

import utility.ConvertToLower;
import utility.Input;

import database.RecordSearch;
import dbmanager.DBManager;
import dbmanager.GetIdName;
//import file.SearchRecord;

public class Protected  implements Scope
{
	DBManager database;
	GetIdName gid;
	private  String PROTECTED="protected";
	Vector<Object> scopeVector = new Vector<Object>();
	String parentobjet;
	String INHEITANCE ="inheritance";
	//SearchRecord sr;
	RecordSearch ps;
	
	public Vector<Object> getVector() 
	{
		return scopeVector;
	}



	public void processScope() 
	{
		Hashtable<Object,Object>temphashtable = new Hashtable<Object, Object>();
		Vector<Object>tempvector = new Vector<Object>();
		tempvector.addAll(scopeVector);
		for(int i=0;i<scopeVector.size();i++)
		{
			Hashtable<Object, Object>table =(Hashtable<Object, Object>) scopeVector.get(i);
			table = ConvertToLower.convertHashKey(table);
			if(table.get("pv").toString().equals(PROTECTED) )
			{	
				/*String path = Input.MASTERREPORTSPATH + parentobjet + "//" + parentobjet  + "_mid" + Input.FILRFORMAT;
				sr = new SearchRecord(path);*/
				temphashtable.clear();
				temphashtable.put("pid", table.get("ref").toString());
				temphashtable.put("mid", parentobjet);
				/*sr.fileOpen();
				Hashtable<Object, Object> result = sr.getRecordHash(temphashtable);
				sr.fileClose();*/
				temphashtable.put("td","null");
				ps = new RecordSearch(database);
				ps.setConditionMap(temphashtable);
				Hashtable<Object, Object> result = ps.getHashFromResultSet();
				
				String protectedmember  = table.get("ref").toString();
				int index=-1;
				Hashtable<Object, Object >  tempvalue =(Hashtable<Object, Object>) tempvector.get(i);
				for(int j=0;j<tempvector.size();j++)
				{
					Hashtable<Object, Object>temptable =(Hashtable<Object, Object>) tempvector.get(j);
					if(temptable.get("pid").equals(protectedmember))
					{
						index = j;
						break;
					}
				}
				try
				{
					tempvector.remove(index);
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				} 
				int k=tempvector.indexOf(tempvalue);
				tempvector.remove(k);
				tempvector.add(result);
			}	
		}
		scopeVector.clear();
		scopeVector.addAll(tempvector);
	}



	public void setDbmanger(DBManager database) 
	{
	  this.database = database;
	  gid = new GetIdName(database);
	  PROTECTED = gid.getId(PROTECTED);
	  INHEITANCE = gid.getId(INHEITANCE);
	}



	public void setVector(Vector<Object> scopeVector) 
	{	
	 this.scopeVector = scopeVector;	
	}



	public void getParent(String parentobject) 
	{
	 this.parentobjet = parentobject;
	}
	

	
}
